package idv.loveloser.servlet;

import idv.loveloser.dao.MdlAssignmentSubmissions;
import idv.loveloser.dao.MdlAssignmentSubmissionsDAO;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class AddCourseOnlineAssiment extends HttpServlet {
	private MdlAssignmentSubmissionsDAO submissionDAO = new MdlAssignmentSubmissionsDAO();
	/**
	 * Constructor of the object.
	 */
	public AddCourseOnlineAssiment() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
		String assignmentId = request.getParameter("assignmentId");
		String assignmentContent = request.getParameter("content");
		MdlAssignmentSubmissions submission=null;
		if(Util.isAuthorised(client, sessionKey)){
			List<MdlAssignmentSubmissions> submissions= submissionDAO.findByAssignment(Long.parseLong(assignmentId));
			if(submissions.size()>0){
				submission = submissions.get(0);
				submission.setData1(assignmentContent);
				submission.setData2("1");
				submission.setTimemodified(System.currentTimeMillis());
				submissionDAO.merge(submission);
			}else{
				submission=new MdlAssignmentSubmissions();
				submission.setAssignment(Long.parseLong(assignmentId));
				submission.setData1(assignmentContent);
				submission.setData2("1");
				submission.setFormat((short)0);
				submission.setGrade(-1L);
				submission.setMailed(false);
				submission.setNumfiles(0L);
				submission.setSubmissioncomment("");
				submission.setTeacher(0L);
				submission.setTimecreated(System.currentTimeMillis());
				submission.setTimemarked(0L);
				submission.setTimemodified(System.currentTimeMillis());
				submission.setUserid(Util.getUserId(Long.parseLong(client)));
				submissionDAO.save(submission);
			}
		}
		Gson gson = new Gson();
		out.println(gson.toJson(submission));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
